package com.jlc.book.shop.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jlc.book.shop.delegate.BookDelegate;
import com.jlc.book.shop.to.BookTO;
import com.jlc.book.shop.validator.JLCDataValidator;

public class AddBookAction {


	public String addBookInfo(HttpServletRequest request,
			HttpServletResponse response) {
		
		String page="addBookDef.jsp";
		String bnm=request.getParameter("bname");
		String author=request.getParameter("author");
		String cost=request.getParameter("cost");
		String pub=request.getParameter("publication");
		String edi=request.getParameter("edition");
		
		float bcost=0.0f;
		
		boolean convertCost=true;
		boolean intactWithDB=true;
		
		if(!JLCDataValidator.validateRequired(bnm))
			
		{
			request.setAttribute("bname", "Book name is required");
			intactWithDB=false;
		}
		else if(!JLCDataValidator.minLength(bnm, 3))
		{
			request.setAttribute("bname", "Book name  must be minimum 3 character");
			intactWithDB=false;
		}
		else if(!JLCDataValidator.maxLength(bnm, 60))
		{
			request.setAttribute("bname", "Book name  must be maximum 60 character");
			intactWithDB=false;
		}
		
		
        if(!JLCDataValidator.validateRequired(author))
			
		{
			request.setAttribute("author", "Author name is required");
			intactWithDB=false;
		}
		else if(!JLCDataValidator.minLength(bnm, 3))
		{
			request.setAttribute("author", "Author name  must be minimum 3 character");
			intactWithDB=false;
		}
		else if(!JLCDataValidator.maxLength(bnm, 50))
		{
			request.setAttribute("author", "Author name  must be maximum 60 character");
			intactWithDB=false;
		}
		
        if(!JLCDataValidator.validateRequired(cost))
			
		{
			request.setAttribute("cost", "Cost is required");
			intactWithDB=false;
			convertCost=false;
		}

     if(!JLCDataValidator.validateRequired(edi))
	
{
	request.setAttribute("edition", "Edition is required");
	intactWithDB=false;
}
		
		
     if(!JLCDataValidator.validateRequired(pub))
			
		{
			request.setAttribute("publication", "Publication is required");
			intactWithDB=false;
		}
		else if(!JLCDataValidator.minLength(bnm, 3))
		{
			request.setAttribute("publication", "Publication name  must be minimum 3 character");
			intactWithDB=false;
		}
		else if(!JLCDataValidator.maxLength(bnm, 50))
		{
			request.setAttribute("publication", "Publication name  must be maximum 50 character");
			intactWithDB=false;
		}
     
     if(convertCost)
     {
    	 try
    	 {
    		 bcost=Float.parseFloat(cost);
    	 }
    	 catch (NumberFormatException e) {
		
    		 request.setAttribute("cost", "Cost is not valid");
    		 intactWithDB=false;
		}
     }
		
     if(intactWithDB)
     {
    	BookTO bto=new BookTO(bnm, author, pub, edi, bcost); 
    	if(BookDelegate.atreayExist(bto))
    	{
    		request.setAttribute("addingBookError", "Book information already available");
    		
    	}
    	else
    	{
    		boolean added=BookDelegate.addBook(bto);
    		if(added)
    		{
    	request.setAttribute("addingBookError", "Book information added successfully");
    	request.setAttribute("SHOW_ADD_BOOK", "OK");
    		}
    		
    		else
    		{
    	request.setAttribute("addingBookError", "Error in adding book information");
    		}
    	}
     }
	
		
		return page;
	}

}
